/*
 * @lc app=leetcode.cn id=9 lang=java
 *
 * [9] 回文数
 */

// @lc code=start
class Solution {
    public boolean isPalindrome(int x) {
        
        // 解法一：转为字符串
        //return numToString(x);


        // 解法二：数值求余取模
        return numCount(x);

    }

    public boolean numToString(int x) {
        if ( x < 0 ) {
            return false;
        }

        // int有3种转String的方式
        String str = Integer.toString(x);
        // String str = x + ""; 
        // String str = String.valueOf(x);
        
        // 5 : 0 - 4
        int start = 0;
        int end = str.length() - 1;

        // 5 2  =  2
        while ( start <= end ) {
            if (str.charAt(start) != str.charAt(end)) {
                return false;
            }
            start ++ ;
            end --;
        }

        return true;
    }

    public boolean numCount(int x) {
        
        // 排除不可能
        if ( x < 0 || ( x % 10 == 0 && x != 0) ) {
            return false;
        }

        int backnum = 0;
        while ( x > backnum ) {
            backnum = backnum * 10 + x % 10;
            x = x / 10;
        }

        // 12321 :  backnum = 123 , x = 12
        if ( backnum / 10 == x || backnum == x ) 
            return true;

        return false;

    }
}
// @lc code=end

